package com.sunda.spmsoversea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.entity.MovementType;
import com.sunda.spmsoversea.mapper.MovementTypeMapper;
import com.sunda.spmsoversea.service.IMovementTypeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 领用移动类型 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2022-01-14
 */
@Service
public class MovementTypeServiceImpl extends ServiceImpl<MovementTypeMapper, MovementType> implements IMovementTypeService {

    @Override
    public List<MovementType> getMovementType() {
        QueryWrapper<MovementType> queryWrapper = new QueryWrapper<MovementType>().ne("MOVEMENT_TYPE", " ").orderByAsc("MOVEMENT_TYPE");
        List<MovementType> movementTypeList = this.baseMapper.selectList(queryWrapper);
        return movementTypeList;
    }

    @Override
    public synchronized ResponseResult createMovementType(MovementType movementType, String userId) {
        if (movementType == null){
            return ResponseResult.error("请求参数错误");
        }
        String uuid = movementType.getUuid();
        String movementTypeCode = movementType.getMovementType();
        String movementTypeZhDesc = movementType.getMovementTypeZhDesc();
        String movementTypeEnDesc = movementType.getMovementTypeEnDesc();
        if (StringUtils.isEmpty(uuid)){
            if (StringUtils.isEmpty(movementTypeCode) || StringUtils.isEmpty(movementTypeZhDesc)
                    || StringUtils.isEmpty(movementTypeEnDesc)){
                return ResponseResult.error("新建移动类型参数不完整");
            }
            QueryWrapper<MovementType> queryWrapper = new QueryWrapper<MovementType>().eq("MOVEMENT_TYPE", movementTypeCode);
            MovementType existMovementType = this.baseMapper.selectOne(queryWrapper);
            if (existMovementType != null){
                return ResponseResult.error("移动类型" + movementTypeCode + "已存在，请勿重复建");
            }
            MovementType addMovementType = new MovementType();
            String newUuid = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
            addMovementType.setUuid(newUuid);
            addMovementType.setMovementType(movementTypeCode);
            addMovementType.setMovementTypeZhDesc(movementTypeZhDesc);
            addMovementType.setMovementTypeEnDesc(movementTypeEnDesc);
            addMovementType.setApplicableBusiness(movementType.getApplicableBusiness());
            addMovementType.setRemarks(movementType.getRemarks());
            this.baseMapper.insert(addMovementType);
            return ResponseResult.success("新增成功").add("movementTypeList", getMovementType());
        }
        QueryWrapper<MovementType> queryWrapperUuid = new QueryWrapper<MovementType>().eq("UUID", uuid);
        MovementType movementType1 = this.baseMapper.selectOne(queryWrapperUuid);
        if (movementType1 == null){
            return ResponseResult.error("请求更新移动类型数据不存在请刷新重试");
        }
        movementType1.setMovementTypeZhDesc(movementTypeZhDesc);
        movementType1.setMovementTypeEnDesc(movementTypeEnDesc);
        movementType1.setApplicableBusiness(movementType.getApplicableBusiness());
        movementType1.setRemarks(movementType.getRemarks());
        this.baseMapper.update(movementType1, queryWrapperUuid);
        return ResponseResult.success("更新成功").add("movementTypeList", getMovementType());
    }

    @Override
    public ResponseResult deleteMovementType(String uuid, String userId) {
        if (StringUtils.isEmpty(uuid)){
            return ResponseResult.success("删除失败").add("movementTypeList", getMovementType());
        }
        QueryWrapper<MovementType> queryWrapper = new QueryWrapper<MovementType>().eq("UUID", uuid);
        this.baseMapper.delete(queryWrapper);
        return ResponseResult.success("删除成功").add("movementTypeList", getMovementType());
    }
}
